#!/usr/bin/env php
<?php

# A worker that tracks URLs that have been stored and also
# searches through them.

$pages = array();
$worker = new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('store_url', 'store_url_callback');
$worker->addFunction('search_url', 'search_url_callback');
while (@$worker->work());

function store_url_callback($job)
{
  $page = json_decode($job->workload(), true);
  array_push($GLOBALS['pages'], $page);
}

function search_url_callback($job)
{
  $result = '';

  foreach ($GLOBALS['pages'] as $page)
  {
    if (strstr($page['body'], $job->workload()))
      $result .= $page['title'] . ' - ' . $page['url'] . "\n";
  }

  return $result;
}

?>
